<?php 
	/**
	 *  用户注册模型
	 */
class UserModel extends Model{
	
	public $table='user';	
	
	public $validate=array(
		array('username','nonull','用户名不能为空',2,3),
		array('password','nonull','密码不能为空',2,3),
		array('password','confirm:password2','两次密码不一致',2,3),
		array('verify','nonull','验证码不能为空',1,3),
	);
	
	public function addData(){
		//执行上面的自动验证规则
	if(!$this->create()) return false;//如果验证码错误，返回false
	//判断验证码是否正确
		$code =strtoupper($_POST['verify']);
		if($code != $_SESSION['code']){
			$this->error = '验证码错误';
			return false;
		}
		//判断用户名是否存在
	$username = htmlspecialchars($_POST['username']);
	$userInfo = $this->where("username='{$username}'")->find();
	if($userInfo){
		$this->error = '用户名已存在';
		return false;
	}
	//如果全部通过则压入数组
	$this->data['username'] = $username;
	$this->data['password'] = md5($_POST['password']);
		return $this->add();
	}
	/*
	 * 前台验证登陆
	 */
	public function login() {

		//判断用户名和密码是否正确
		$username = $_POST['username'];
		$password = md5($_POST['password']);
		//		p($password);die;
		//在数据库查询用户的信息
		$userInfo = $this -> where("username='{$username}'") -> find();
//				p($userInfo);die;
		//用户名或者密码只要有一个不相等就返回false
		if(!$userInfo || $password != $userInfo['password']){
			$this->error = "用户名或者密码错误";
			return false;
		}

		//判断用户是否呗锁定
		if ($userInfo['is_lock']) {
			$this -> error = "您已经呗锁定，请联系管理员";
			return false;
		}

		return $userInfo;

	}
}

 ?>
 